<?php
/**
 * Shop.class.php
 *
 * @author camfee <camfee@foxmail.com>
 * @date   18-1-2 下午9:17
 */

namespace Center;

use Common\ModuleBase;
use Common\Bridge;

class Shop extends ModuleBase
{
    protected static $_conf = [
        'db' => [
            'w' => Bridge::DB_CENTER_W,
            'r' => Bridge::DB_CENTER_R
        ],
        'table' => 'Shop',
        'fields' => [
            'Id' => self::VAR_TYPE_KEY,
            'CityId' => self::VAR_TYPE_INT,
            'CateId' => self::VAR_TYPE_INT,
            'Name' => self::VAR_TYPE_STRING,
            'Keywords' => self::VAR_TYPE_STRING,
            'Cover' => self::VAR_TYPE_STRING,
            'UserId' => self::VAR_TYPE_INT,
            'ActiveId' => self::VAR_TYPE_INT,
            'Description' => self::VAR_TYPE_STRING,
            'Address' => self::VAR_TYPE_STRING,
            'Mobile' => self::VAR_TYPE_STRING,
            'Status' => self::VAR_TYPE_INT,
            'Content' => self::VAR_TYPE_STRING,
            'UpdateTime' => self::VAR_TYPE_STRING,
            'CreateTime' => self::VAR_TYPE_STRING,
            'Members' => self::VAR_TYPE_INT,
            'Brower' => self::VAR_TYPE_INT,
            'Sort' => self::VAR_TYPE_INT,
            'JCoin' => self::VAR_TYPE_INT,
            'MaxJCoin' => self::VAR_TYPE_INT,
            'Abscissa' => self::VAR_TYPE_STRING,
            'Ordinate' => self::VAR_TYPE_STRING,
        ],
        'mc' => Bridge::MEMCACHE_DEFAULT,
        // 可选, MC KEY, "KeyName:%d", %d会用主键ID替代
        'mckey' => 'Shop:%d',
        // 可选, 超时时间, 默认不过期
        'mctime' => 86400,
    ];

    const MC_SEARCH_SHOP = 'SEARCH_SHOP:%s';

    public static function searchShop($str, $offset = 0, $limit = 20)
    {
        $key = sprintf(self::MC_SEARCH_SHOP, base64_encode($str));
        $data = self::getMC()->get($key);
        if (empty($data)) {
            $where = [
                'Keywords LIKE' => '%' . $str . '%',
            ];
            $list_info = self::getList($where, 0, 500, 'Id');
            $data = [
                'count' => intval($list_info['count']),
                'data' => [],
            ];
            if (!empty($list_info['data'])) {
                foreach ($list_info['data'] as $v) {
                    $data['data'][$v['Id']] = $v['Id'];
                }
            }
            self::getMC()->set($key, $data, 300);
        }
        if (!empty($data['data'])) {
            $data['data'] = array_slice($data['data'], $offset, $limit, true);
        }

        return $data;
    }

    /**
     * 获取shop列表
     *
     * @param     $cityid
     * @param     $catid
     * @param int $offset
     * @param int $limit
     * @return array
     */
    public static function getListByCityCatId($cityid, $catid, $offset = 0, $limit = 20)
    {
        $where = [
            'CityId' => $cityid,
            'CateId' => $catid
        ];

        return self::getList($where, $offset, $limit, '*', 'Sort Desc');
    }
    /**
     * @see ModuleBase::add() 新增
     * @see ModuleBase::update() 更新
     * @see ModuleBase::getInfoByIds() 按id查询
     * @see ModuleBase::getList() 条件查询
     * @see ModuleBase::delete() 删除
     */

}